//
//  RootViewController.m
//  Collection
//
//  Created by Home on 9/15/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"
#import "PersonalInfoVC.h"
#import "ProductList.h"
#import "CarSectionTableViewController.h"
#import "imagePickViewController.h"

@implementation RootViewController
@synthesize date;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"Collection";
	NSDate *selected=[NSDate date];
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    [format setDateFormat:@"dd/MM/YYYY"];
	
	date.text = [NSString stringWithFormat:@"%@",[format stringFromDate:selected]];

  //  [self.navigationController.navigationBar setTintColor:[UIColor redColor]];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}


 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}
-(IBAction)buttonGoAction:(id)sender;{
   
	NSString *queryStr =[NSString stringWithFormat:@"http://webservices.dhakera.com/CollectionBookingDetail.aspx?id=%@",txtbookin.text];
	NSLog(@"query str: %@",queryStr);
	NSString *jsonString = [self stringWithUrl:[NSURL URLWithString:[queryStr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];
	NSLog(@"response>>>>>>>> %@",jsonString);
	
	SBJSON *jsonParser = [[[SBJSON alloc] init] autorelease];
	NSArray *arr = [[NSArray alloc] initWithArray:[jsonParser objectWithString:jsonString error:nil]];
	NSLog(@"dic %@",arr);
	if (arr.count>0) {
		PersonalInfoVC *objPersonalInfoVC = [[PersonalInfoVC alloc]initWithWebData:arr];
		[self.navigationController pushViewController:objPersonalInfoVC animated:YES];
		[objPersonalInfoVC release];
		
	}
	else {
		NSLog(@"~~~~~~~~~~~~~~~~~~~Inavalid BookingID");
		PersonalInfoVC *objPersonalInfoVC = [[PersonalInfoVC alloc]init];
		[self.navigationController pushViewController:objPersonalInfoVC animated:YES];
		[objPersonalInfoVC release];
	}

	
	
	//CarSectionTableViewController *carSec = [[CarSectionTableViewController alloc]init];
	//[self.navigationController pushViewController:carSec animated:YES];
	//[carSec release];
	
	//imagePickViewController *carSec = [[imagePickViewController alloc]init];
	//[self.navigationController pushViewController:carSec animated:YES];
	//[carSec release];
	
	
}

-(NSString *)stringWithUrl:(NSURL *)url
{
    NSString *URLString = nil;
	
	@try { 
		
		NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:100];
		
		NSURLResponse *response;
		NSError *error;
		
		// Make synchronous request
		NSData *urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
		
		// Construct a String around the Data from the response
		URLString = [[[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding] autorelease];
	}
	
	@catch (id e) {
		
		NSLog(@"Model stringWithUrl: %@", e);
		@throw;
	}
	
	NSAssert (URLString != nil, @"Invalid value of URLString in +[Model stringWithUrl:]");
	
	return URLString;
    
}

-(bool)stringFromStatus:(NetworkStatus)status
{
	bool internetConn;
	switch(status)
	{
		case NotReachable: 
			
			internetConn=NO;
			UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"No Connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
			[alert show];
			[alert release];
			break; 
		case ReachableViaWiFi:
			internetConn=YES;
			break;
		case ReachableViaWWAN: 
            internetConn=YES;
			break; 
		default:
			internetConn=YES;
			break;
	}
	return internetConn;
}


-(IBAction)buttonMsgAction:(id)sender;
{
	UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Alert" message:[NSString stringWithFormat:@"You only have %@ emails left before subscription expires.  Would you like to subscribe now?",[[NSUserDefaults standardUserDefaults] valueForKey:@"Email"]] delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
	[alert show];
	[alert release];
	
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if (buttonIndex ==1)
	{
		ProductList *proList=[[ProductList alloc] init];
		[self.navigationController pushViewController:proList animated:YES ];
		[proList release];
		
	}
}



-(BOOL)textFieldShouldReturn:(UITextField *)txtField{
    [txtField resignFirstResponder];
    return YES;
}
- (void)dealloc
{
	[date release];
    [super dealloc];
}

@end
